<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>位运算符 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="chapter08.html" />
    
    
    <link rel="prev" href="chapter06.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="7"
        data-chapter-title="位运算符"
        data-filepath="chapter07.md"
        data-basepath="."
        data-revision="Mon Feb 03 2020 12:30:22 GMT+0800 (中国标准时间)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        整数类型操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        数据类型转换
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        算数运算符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        赋值运算符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        比较运算符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6" data-path="chapter06.html">
            
                
                    <a href="chapter06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        逻辑运算符
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="7" data-path="chapter07.html">
            
                
                    <a href="./chapter07.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        位运算符
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="chapter08.html">
            
                
                    <a href="chapter08.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        运算符优先级
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="&#x4F4D;&#x8FD0;&#x7B97;&#x7B26;&#xFF08;&#x4E86;&#x89E3;&#xFF09;">&#x4F4D;&#x8FD0;&#x7B97;&#x7B26;&#xFF08;&#x4E86;&#x89E3;&#xFF09;</h2>
<p>&#x6309;&#x4F4D;&#x8FD0;&#x7B97;&#x7B26;&#x662F;&#x628A;&#x6570;&#x5B57;&#x770B;&#x4F5C;&#x662F;&#x4E8C;&#x8FDB;&#x5236;&#x6765;&#x8FDB;&#x884C;&#x8BA1;&#x7B97;&#x7684;&#x3002;
&#x4E0B;&#x8868;&#x4E2D;&#x53D8;&#x91CF; a &#x4E3A; 60&#xFF0C;b &#x4E3A; 13&#xFF0C;&#x4E8C;&#x8FDB;&#x5236;&#x683C;&#x5F0F;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>a = 0011 1100  # 60&#x7684;&#x4E8C;&#x8FDB;&#x5236;
b = 0000 1101  # 13&#x7684;&#x4E8C;&#x8FDB;&#x5236;
-----------------
a&amp;b = 0000 1100
a|b = 0011 1101
a^b = 0011 0001
~a  = 1100 0011
</code></pre><table>
<thead>
<tr>
<th>&#x4F4D;&#x8FD0;&#x7B97;</th>
<th>&#x63CF;&#x8FF0;</th>
<th>&#x793A;&#x4F8B;</th>
</tr>
</thead>
<tbody>
<tr>
<td>&amp;</td>
<td>&#x6309;&#x4F4D;&#x4E0E;&#x8FD0;&#x7B97;&#x7B26;&#xFF1A;&#x53C2;&#x4E0E;&#x8FD0;&#x7B97;&#x7684;&#x4E24;&#x4E2A;&#x503C;,&#x5982;&#x679C;&#x4E24;&#x4E2A;&#x76F8;&#x5E94;&#x4F4D;&#x90FD;&#x4E3A;1,&#x5219;&#x8BE5;&#x4F4D;&#x7684;&#x7ED3;&#x679C;&#x4E3A;1,&#x5426;&#x5219;&#x4E3A;0</td>
<td>(a &amp; b) &#x8F93;&#x51FA;&#x7ED3;&#x679C; 12 &#xFF0C;&#x4E8C;&#x8FDB;&#x5236;&#x89E3;&#x91CA;&#xFF1A; 0000 1100</td>
</tr>
<tr>
<td>|</td>
<td>&#x6309;&#x4F4D;&#x6216;&#x8FD0;&#x7B97;&#x7B26;&#xFF1A;&#x53EA;&#x8981;&#x5BF9;&#x5E94;&#x7684;&#x4E8C;&#x4E2A;&#x4E8C;&#x8FDB;&#x4F4D;&#x6709;&#x4E00;&#x4E2A;&#x4E3A;1&#x65F6;&#xFF0C;&#x7ED3;&#x679C;&#x4F4D;&#x5C31;&#x4E3A;1&#x3002;</td>
<td>(a | b) &#x8F93;&#x51FA;&#x7ED3;&#x679C; 61 &#xFF0C;&#x4E8C;&#x8FDB;&#x5236;&#x89E3;&#x91CA;&#xFF1A; 0011 1101</td>
</tr>
<tr>
<td>^</td>
<td>&#x6309;&#x4F4D;&#x5F02;&#x6216;&#x8FD0;&#x7B97;&#x7B26;&#xFF1A;&#x5F53;&#x4E24;&#x5BF9;&#x5E94;&#x7684;&#x4E8C;&#x8FDB;&#x4F4D;&#x76F8;&#x5F02;&#x65F6;&#xFF0C;&#x7ED3;&#x679C;&#x4E3A;1</td>
<td>a ^ b) &#x8F93;&#x51FA;&#x7ED3;&#x679C; 49 &#xFF0C;&#x4E8C;&#x8FDB;&#x5236;&#x89E3;&#x91CA;&#xFF1A; 0011 0001</td>
</tr>
<tr>
<td>~</td>
<td>&#x6309;&#x4F4D;&#x53D6;&#x53CD;&#x8FD0;&#x7B97;&#x7B26;&#xFF1A;&#x5BF9;&#x6570;&#x636E;&#x7684;&#x6BCF;&#x4E2A;&#x4E8C;&#x8FDB;&#x5236;&#x4F4D;&#x53D6;&#x53CD;,&#x5373;&#x628A;1&#x53D8;&#x4E3A;0,&#x628A;0&#x53D8;&#x4E3A;1 &#x3002;~x &#x7C7B;&#x4F3C;&#x4E8E; -x-1</td>
<td>(~a ) &#x8F93;&#x51FA;&#x7ED3;&#x679C; -61 &#xFF0C;&#x4E8C;&#x8FDB;&#x5236;&#x89E3;&#x91CA;&#xFF1A; 1100 0011&#xFF0C;&#x5728;&#x4E00;&#x4E2A;&#x6709;&#x7B26;&#x53F7;&#x4E8C;&#x8FDB;&#x5236;&#x6570;&#x7684;&#x8865;&#x7801;&#x5F62;&#x5F0F;&#x3002;</td>
</tr>
<tr>
<td>&lt;&lt;</td>
<td>&#x5DE6;&#x79FB;&#x52A8;&#x8FD0;&#x7B97;&#x7B26;&#xFF1A;&#x8FD0;&#x7B97;&#x6570;&#x7684;&#x5404;&#x4E8C;&#x8FDB;&#x4F4D;&#x5168;&#x90E8;&#x5DE6;&#x79FB;&#x82E5;&#x5E72;&#x4F4D;&#xFF0C;&#x7531; <strong>&lt;&lt;</strong> &#x53F3;&#x8FB9;&#x7684;&#x6570;&#x5B57;&#x6307;&#x5B9A;&#x4E86;&#x79FB;&#x52A8;&#x7684;&#x4F4D;&#x6570;&#xFF0C;&#x9AD8;&#x4F4D;&#x4E22;&#x5F03;&#xFF0C;&#x4F4E;&#x4F4D;&#x8865;0&#x3002;</td>
<td>a &lt;&lt; 2 &#x8F93;&#x51FA;&#x7ED3;&#x679C; 240 &#xFF0C;&#x4E8C;&#x8FDB;&#x5236;&#x89E3;&#x91CA;&#xFF1A; 1111 0000</td>
</tr>
<tr>
<td>&gt;&gt;</td>
<td>&#x53F3;&#x79FB;&#x52A8;&#x8FD0;&#x7B97;&#x7B26;&#xFF1A;&#x628A;&quot;&gt;&gt;&quot;&#x5DE6;&#x8FB9;&#x7684;&#x8FD0;&#x7B97;&#x6570;&#x7684;&#x5404;&#x4E8C;&#x8FDB;&#x4F4D;&#x5168;&#x90E8;&#x53F3;&#x79FB;&#x82E5;&#x5E72;&#x4F4D;&#xFF0C;<strong>&gt;&gt;</strong> &#x53F3;&#x8FB9;&#x7684;&#x6570;&#x5B57;&#x6307;&#x5B9A;&#x4E86;&#x79FB;&#x52A8;&#x7684;&#x4F4D;&#x6570;</td>
<td>a &gt;&gt; 2 &#x8F93;&#x51FA;&#x7ED3;&#x679C; 15 &#xFF0C;&#x4E8C;&#x8FDB;&#x5236;&#x89E3;&#x91CA;&#xFF1A; 0000 1111</td>
</tr>
</tbody>
</table>
<h2 id="&#x7EC3;&#x4E60;">&#x7EC3;&#x4E60;</h2>
<p>&#x4EE5;&#x4E0B;&#x7EC3;&#x4E60;&#x96BE;&#x5EA6;&#x8F83;&#x9AD8;&#xFF0C;&#x53EA;&#x9700;&#x8981;&#x80FD;&#x770B;&#x61C2;&#x4EE3;&#x7801;&#x5373;&#x53EF;&#xFF0C;&#x4E0D;&#x8981;&#x6C42;&#x5199;&#x51FA;&#x3002;</p>
<ul>
<li>&#x4F7F;&#x7528;&#x4F4D;&#x8FD0;&#x7B97;&#xFF0C;&#x83B7;&#x53D6;&#x5230;&#x5341;&#x516D;&#x8FDB;&#x5236;&#x989C;&#x8272; <code>0xF0384E</code> &#x7684;RGB&#x503C;&#xFF0C;&#x4EE5;&#x5341;&#x8FDB;&#x5236;&#x5F62;&#x5F0F;&#x6253;&#x5370;&#x8F93;&#x51FA;&#x3002;</li>
</ul>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="chapter06.html" class="navigation navigation-prev " aria-label="Previous page: 逻辑运算符"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="chapter08.html" class="navigation navigation-next " aria-label="Next page: 运算符优先级"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
